/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package icexam.exam.print;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRElement;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRReport;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.base.JRBaseLine;
import net.sf.jasperreports.engine.base.JRBasePrintText;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JRDesignBand;
import net.sf.jasperreports.engine.design.JRDesignExpression;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.design.JRDesignImage;
import net.sf.jasperreports.engine.design.JRDesignStaticText;
import net.sf.jasperreports.engine.design.JRDesignTextField;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.type.HorizontalAlignEnum;
import net.sf.jasperreports.engine.type.ScaleImageEnum;
import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;

/**
 *
 * @author Ethan
 */
public class ReportProcessX extends ReportProcess{

    private final static int textWidth = 80;
    private final static int textHeight = 12;
    private final static int columnHeaderfontSize = 8;
    private final static int fontSize = 7;
    private final static int X = 80;
    private final static int Y = 12;
    private final static int columnHeaderHeight = 12 * 3;
    private final static int columnHeaderFooter = 12 * 3;
    private final static int detailHeight = 22;
    private static String aliasColumn = "column";

    private List<JasperPrint> jasperPrints = new ArrayList<JasperPrint>();

    public void addPreview(int type,String headers[], String title1[], String title2[], List<Object[]> list, List<Object[]> footlist,String arg0,String arg1,String arg2) throws Exception {
        String[] alias = preaseAliasColumnHeaders(headers);

        JasperReport jp = getJasperReport(type,headers, alias, title1, title2,footlist,arg0,arg1,arg2);
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("imageParam", "D:/logo.jpg");
        parameters.put("isShowImage", "true");
        JasperPrint jasperPrint = JasperFillManager.fillReport(jp, parameters,
                new JRBeanCollectionDataSource(getBaseList(alias, phrase(list))));
        jasperPrints.add(jasperPrint);
    }

    public static List<String[]> phrase(List<Object[]> list) {
        List<String[]> temps = new ArrayList<String[]>();
        String[] s = null;
        for (Object[] obj : list) {
            s = new String[obj.length];
            for (int i = 0; i < obj.length; i++) {
                if(obj[i]!=null){
                    s[i] = obj[i].toString();
                }else
                    s[i] = "";

            }
            temps.add(s);
        }
        return temps;
    }


    private static String[] preaseAliasColumnHeaders(String headers[]) {
        int size = headers.length;
        String[] alias = new String[size];
        for (int i = 0; i < size; i++) {
            alias[i] = aliasColumn + i;
        }
        return alias;
    }
    private static int[] wightArray = {20,400,55,55,40};

    @SuppressWarnings("deprecation")
    private static JasperReport getJasperReport(int type,String[] headers,
            String alias[], String[] title1, String[] title2,List<Object[]> footlist,String reportName,String reportTime,String score) throws JRException {
        JasperDesign design = new JasperDesign();
        design.setName("statistics");
        design.setPrintOrder(JRReport.PRINT_ORDER_VERTICAL);
        design.setOrientation(JRReport.ORIENTATION_LANDSCAPE);
        design.setPageWidth(842);
        design.setPageHeight(595);
        design.setColumnWidth(800);
        design.setColumnSpacing(0);
        design.setLeftMargin(15);
        design.setRightMargin(0);
        design.setTopMargin(20);
        design.setBottomMargin(20);
        design.setWhenNoDataType(JRReport.WHEN_NO_DATA_TYPE_BLANK_PAGE);
        design.setTitleNewPage(false);
        design.setSummaryNewPage(false);

        JRDesignBand columnHeader = new JRDesignBand();
        columnHeader.setHeight(200);
     
        JRDesignStaticText staticText = new JRDesignStaticText();
//        if (headers.length >= 5) {
            staticText.setText(reportName);
            staticText.setFontSize(50);
            staticText.setHeight(100);
            staticText.setWidth(700);
            staticText.setBold(true);
            staticText.setX(50);
            staticText.setY(100);
//            staticText.setBackcolor(new Color(200, 255, 200));
            staticText.setMode(JRElement.MODE_OPAQUE);
            staticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);
            columnHeader.addElement(staticText);
            
            


            
        JRDesignBand band = new JRDesignBand();
        band.setHeight(550);
        String imgPath = "\"bg.jpg\"";
        JRDesignExpression jexpression = new JRDesignExpression();
        jexpression.setValueClass(String.class);
        jexpression.setText(imgPath);
        JasperDesign jasperDesign = new JasperDesign();
        JRDesignImage image = new JRDesignImage(jasperDesign);
        image.setX(5);
        image.setY(5);
        image.setWidth(800);
        image.setHeight(540);

        //We can use another scaling mode
        image.setScaleImage(ScaleImageEnum.FILL_FRAME);
        image.setExpression(jexpression);

        //Adds image to the band
        band.addElement(image);
        design.setBackground(band);
        design.setColumnHeader(columnHeader);
        JRDesignBand detail1 = new JRDesignBand();
        detail1.setHeight(150);
        staticText = new JRDesignStaticText();

        JRDesignStaticText title2Text = (JRDesignStaticText) staticText.clone();
        title2Text.setText(title2[0]);
        title2Text.setHeight(textHeight * 2);
        title2Text.setY(textHeight );
        JRDesignStaticText title3Text = (JRDesignStaticText) staticText.clone();
        title3Text.setText(headers[0]);
        title3Text.setHeight(textHeight);
        title3Text.setY(textHeight * 3);
        JRDesignField field = new JRDesignField();
        field.setName(alias[0]);
        field.setValueClass(String.class);
        design.addField(field);

        // add text fields for displaying fields
        JRDesignTextField textField = new JRDesignTextField();
        JRDesignExpression expression = new JRDesignExpression();
        expression.setText("$F{" + alias[0] + "}");
        expression.setValueClass(String.class);
        textField.setExpression(expression);
        textField.setHeight(70);
//        textField.setTopPadding(100);
        textField.setFontSize(25);
//        textField.setBold(true);
        textField.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_LEFT);
        textField.setX(80);
        textField.setY(30);
        textField.setWidth(670);
//        textField.setX(x);
        textField.setPdfFontName("STSong-Light");
        textField.setPdfEmbedded(true);
        textField.setPdfEncoding("UniCNS-UCS2-H");
        detail1.addElement(textField);
        design.setDetail(detail1);




        JRDesignBand columnFooter = new JRDesignBand();
        columnFooter.setHeight(120);
        JRDesignStaticText fstaticText = null;
        fstaticText = new JRDesignStaticText();
        fstaticText.setText(reportTime);
        fstaticText.setFontSize(20);
        fstaticText.setY(30);
        fstaticText.setX(500);
        fstaticText.setHeight(80);
        fstaticText.setWidth(200);
        fstaticText.setBold(true);

        fstaticText.setMode(JRElement.MODE_OPAQUE);
        fstaticText.setTextAlignment(JRBasePrintText.HORIZONTAL_ALIGN_CENTER);
        columnFooter.addElement(fstaticText);
        design.setSummary(columnFooter);




        return JasperCompileManager.compileReport(design);
    }
    public static void main(String[] args){
        try {
            String[][] titles = new String[3][3];
            titles[0][0] = "合格证书";
            titles[0][1] = "容县工商行政管理局 2012年12月22日";
            String[][] objs = new String[1][3];
            objs[0][0] = "      飞哥 参加我局举办的容县流通环节食品经营知识培训，并通过了测试，特发此证。";
            ReportViewAction.action(titles, objs,3);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    /**
     * 利用反射機制，裝拼數�?     *
     * @param headers
     * @param list
     * @return
     * @throws Exception
     */
    private static List<Object> getBaseList(String[] headers,
            List<String[]> list) throws Exception {
        List<Object> result = new ArrayList<Object>();
        int length = headers.length;
        DynaProperty[] dynaProps = new DynaProperty[length];
        for (int i = 0; i < length; i++) {
            dynaProps[i] = new DynaProperty(headers[i], String.class);
        }
        BasicDynaClass dynaClass = new BasicDynaClass("first",
                BasicDynaBean.class, dynaProps);
        for (Object[] obj : list) {
            DynaBean employee = dynaClass.newInstance();
            for (int i = 0; i < length; i++) {
                employee.set(headers[i], obj[i]);
            }
            result.add(employee);
        }
        return result;
    }

    public void showAll() {
        new MyReportViewer(jasperPrints);
    }

}
